package com.company.algo.niuke;

import java.util.HashMap;
import java.util.Map;

/**
 * 关键点：if (map.containsKey(target-numbers[i])) return new int[]{map.get(target-numbers[i])+1,i+1};
 */
public class TwoNumSum {
    public int[] twoSum(int[] numbers, int target){
        Map<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < numbers.length; i++) {
            if (map.containsKey(target-numbers[i])) return new int[]{map.get(target-numbers[i])+1,i+1};
            map.put(numbers[i],i);
        }
        throw new RuntimeException("result not found");
    }
}
